<template>
  <div>
    <div>
      <el-input v-model="pos.name" size="small" placeholder="添加职位..." suffix-icon="el-icon-plus"
                class="addPosInput" @keydown.enter.native="addPosition"/>
      <el-button type="primary" icon="el-icon-plus" size="small" @click="addPosition">添加职位</el-button>
    </div>

    <div class="posManaMain">
      <el-table size="small"
                :data="positions"
                border
                stripe
                style="width: 70%"
                @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>

        <el-table-column
            prop="id"
            label="编号"
            width="180">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职位"
            width="180">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建时间">
        </el-table-column>
        <el-table-column
            prop="enabled"
            label="是否启用">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="showEditView(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-button size="small" style="margin-top: 8px" type="danger"
               @click="deleteMany" :disabled="this.multipleSelection.length  === 0">批量删除
    </el-button>

    <el-dialog
        title="编辑职位"
        :visible.sync="dialogVisible"
        width="30%">
      <table>
        <tr>
          <td>
            <el-tag>职位名称</el-tag>
          </td>
          <td>
            <el-input v-model="updatePos.name" size="small" class="updatePosInput"/>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>是否启用</el-tag>
          </td>
          <td>
            <el-switch
                style="margin-left: 6px"
                v-model="updatePos.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已启用"
                inactive-text="未启用">
            </el-switch>
          </td>
        </tr>
      </table>

      <div>


      </div>
      <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdatePos">确 定</el-button>
          </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "PosMana",
  data() {
    return {
      // 绑定的职位数据输入框
      pos: {
        name: ''
      },
      // 点击编辑跳出框绑定数据
      updatePos: {
        name: '',
        enabled: false
      },
      //  表格绑定的所有职位数据
      positions: [],
      // 控制编辑填出框
      dialogVisible: false,
      // 多选框绑定的数据
      multipleSelection: []
    }
  },
  mounted() {
    this.initPositions();
  },
  methods: {
    // 初始化职位
    initPositions() {
      this.getRequest('/system/basic/pos/').then(resp => {
        if (resp) {
          this.positions = resp;
        }
      })
    },
    // 添加职位
    addPosition() {
      if (this.pos.name) {
        this.postRequest('/system/basic/pos/', this.pos).then(resp => {
          if (resp) {
            this.initPositions();
          }
        })
      }
    },
    // 选中编辑
    showEditView(index, data) {
      Object.assign(this.updatePos, data);
      this.dialogVisible = true;
    },
    // 选中删除
    handleDelete(index, data) {
      this.$confirm('此操作将永久删除[' + data.name + ']职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/pos/' + data.id).then(resp => {
          if (resp) {
            this.initPositions();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 修改职位
    doUpdatePos() {
      this.putRequest('/system/basic/pos/', this.updatePos).then(resp => {
        if (resp) {
          this.initPositions();
          this.dialogVisible = false;
        }
      })
    },
    // 批量删除
    deleteMany() {
      this.$confirm('此操作将永久删除[' + this.multipleSelection.length + ']条职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.multipleSelection.forEach(item => {
          ids += 'ids=' + item.id + '&';
        })
        this.deleteRequest('/system/basic/pos/' + ids).then(resp => {
          if (resp) {
            this.initPositions();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      })
    },
    // 选择框发送变化时
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
}
</script>

<style scoped>
.addPosInput {
  width: 300px;
  margin-right: 10px;
}

.posManaMain {
  margin-top: 10px;
}

.updatePosInput {
  width: 70%;
  margin-left: 5px;
}
</style>
